package com.vehiclemanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vehiclemanagement.entity.FuelCard;
import com.vehiclemanagement.service.FuelCardService;
import com.vehiclemanagement.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 加油卡管理控制器
 */
@RestController
@RequestMapping("/fuelcard")
public class FuelCardController {
    
    @Autowired
    private FuelCardService fuelCardService;
    
    /**
     * 分页查询加油卡列表
     */
    @GetMapping("/list")
    public R<IPage<FuelCard>> list(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "searchTerm", required = false) String searchTerm,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "includeDeleted", required = false) Boolean includeDeleted) {
        // 保留includeDeleted参数以兼容前端代码，但实际改为物理删除后该参数不再有实际作用
        IPage<FuelCard> page = fuelCardService.pageFuelCards(current, size, searchTerm, status, includeDeleted);
        return R.ok(page);
    }
    
    /**
     * 获取加油卡详情
     */
    @GetMapping("/{id}")
    public R<FuelCard> getById(@PathVariable Long id) {
        FuelCard fuelCard = fuelCardService.getFuelCardById(id);
        if (fuelCard == null) {
            return R.error("加油卡不存在");
        }
        return R.ok(fuelCard);
    }
    
    /**
     * 添加加油卡
     */
    @PostMapping
    public R<Boolean> add(@RequestBody FuelCard fuelCard) {
        System.out.println("接收到添加加油卡请求，数据：" + fuelCard);
        
        // 检查必要字段
        if (fuelCard.getCardNumber() == null || fuelCard.getCardNumber().trim().isEmpty()) {
            return R.error("卡号不能为空");
        }
        
        boolean result = fuelCardService.addFuelCard(fuelCard);
        if (result) {
            return R.ok(true);
        } else {
            System.out.println("添加加油卡失败，服务层返回false");
            return R.error("添加失败，请检查卡号是否已存在");
        }
    }
    
    /**
     * 更新加油卡
     */
    @PutMapping
    public R<Boolean> update(@RequestBody FuelCard fuelCard) {
        if (fuelCard.getId() == null) {
            return R.error("加油卡ID不能为空");
        }
        boolean result = fuelCardService.updateFuelCard(fuelCard);
        return result ? R.ok(true) : R.error("更新失败");
    }
    
    /**
     * 删除加油卡（物理删除）
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        System.out.println("接收到删除加油卡请求，ID: " + id);
        
        boolean result = fuelCardService.deleteFuelCard(id);
        
        if (result) {
            System.out.println("成功删除加油卡，ID: " + id);
            return R.ok(true);
        } else {
            System.out.println("删除加油卡失败，ID: " + id);
            return R.error("删除失败");
        }
    }
    
    /**
     * 获取卡类型列表
     */
    @GetMapping("/types")
    public R<List<String>> getCardTypes() {
        List<String> types = fuelCardService.getCardTypes();
        return R.ok(types);
    }
    
    /**
     * 诊断接口 - 检查卡号是否存在
     */
    @GetMapping("/check/{cardNumber}")
    public R<Map<String, Object>> checkCardNumber(@PathVariable String cardNumber) {
        Map<String, Object> result = new HashMap<>();
        
        // 检查活跃卡
        boolean existsActive = fuelCardService.existsActiveCardWithNumber(cardNumber);
        result.put("existsActive", existsActive);
        
        // 查询相关记录
        LambdaQueryWrapper<FuelCard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FuelCard::getCardNumber, cardNumber);
        
        List<FuelCard> cards = fuelCardService.list(queryWrapper);
        result.put("records", cards);
        
        return R.ok(result);
    }
    
    /**
     * 测试接口
     */
    @GetMapping("/test")
    public R<String> test() {
        return R.ok("加油卡管理API正常运行！");
    }
} 